package cn.com.dhc.danlu.widget.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.danlu.dlsms.model.pub.res.MessageExtension;

import cn.com.dhc.danlu.R;
import cn.com.dhc.danlu.activity.base.WebViewActivity;
import cn.com.dhc.danlu.activity.coupon.DealersCouponUsedActivity;
import cn.com.dhc.danlu.activity.envelope.DanluEnvelopeActivity;
import cn.com.dhc.danlu.activity.message.OtherDetailMsgActivity;
import cn.com.dhc.danlu.constant.MsgTypeConst.MsgTypeGroup;
import cn.com.dhc.danlu.constant.UrlConst;
import cn.com.dhc.danlu.util.CommonUtils;
import cn.com.dhc.danlu.util.UrlUtil;
import cn.com.dhc.danlu.widget.couponview.filterview.FilterType;

/**
 * @author wuhaiyang 其他消息（红包消息）列表item适配器
 */
public class OtherMsgListAdapter extends MyBaseAdapter<Object> {

    public OtherMsgListAdapter(Context context) {
        super(context);
    }

    @Override
    protected View createView(int position, View convertView, ViewGroup parent) {
        MessageExtension extension = (MessageExtension) getItem(position);
        ViewHolder holder;
        if (null == convertView) {
            convertView = mInflater.inflate(R.layout.item_other_msg, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        boolean isShowTime = true;
        final MessageExtension data = (MessageExtension) getItem(position);
        MessageExtension lastMsgModel = null;
        if (position > 0) {
            lastMsgModel = (MessageExtension) getItem(position - 1);
        }
        if (null != lastMsgModel && CommonUtils.isSameDay(lastMsgModel.getBizCreatedTimestamp(), data.getBizCreatedTimestamp())) {
            isShowTime = false;
        }
        holder.viewToken.setVisibility(isShowTime ? View.GONE : View.VISIBLE);
        if (isShowTime) {
            holder.tvTime.setText(data.getBizCreatedTimestamp().substring(0, 10));
            holder.tvTime.setVisibility(View.VISIBLE);
        } else {
            holder.tvTime.setVisibility(View.GONE);
        }
        final String type = extension.getType();
        if (type.equals(MsgTypeGroup.DANLU_ENVELOP_UNUNSED_MSG) || type.equals(MsgTypeGroup.DANLU_ENVELOP_CHANCE_MSG) || type.equals(MsgTypeGroup.DANLU_ENVELOP_GRAB_MSG)) {
            holder.tvAction.setText("立即参加");
        } else {
            holder.tvAction.setText("查看详情");
        }
        if (type.startsWith(MsgTypeGroup.DANLU_ENVELOP_MSG)) {
            // 红包消息
            holder.tvTitle.setText("红包消息");
        } else {
            holder.tvTitle.setText("其他消息");
        }
        holder.tvContent.setText(extension.getContent());
        holder.llMore.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isEnvelopMsg = type.startsWith(MsgTypeGroup.DANLU_ENVELOP_MSG);
                Intent intent = null;
                Bundle bundle = new Bundle();
                if (isEnvelopMsg) { // 红包消息
                    switch (type) {
                        case MsgTypeGroup.DANLU_ENVELOP_CHANCE_MSG:
                        case MsgTypeGroup.DANLU_ENVELOP_UNUNSED_MSG:
                        case MsgTypeGroup.DANLU_ENVELOP_GRAB_MSG:
                            // 跳转到活动页面
                            WebViewActivity.startSelf(mInflater.getContext(), CommonUtils.getStringByResId(R.string.activity_center), UrlUtil.getActivityUrl(UrlConst.URL_DEFAULT), false);
                            break;

                        case MsgTypeGroup.DEALER_COUPON_USED_MSG:
                        case MsgTypeGroup.DEALER_COUPON_OBTAIN_MSG:
                            // 2016/10/13 跳转经销商优惠券列表页  lyf
                            DealersCouponUsedActivity.startSelf(mInflater.getContext(), FilterType.dealers_coupon);
                            break;

                        case MsgTypeGroup.DEALER_ENVELOP_USED_MSG:
                        case MsgTypeGroup.DEALER_ENVELOP_OBTAIN_MSG:
                            // 2016/10/13 跳转经销商红包列表页  lyf
                            DealersCouponUsedActivity.startSelf(mInflater.getContext(), FilterType.dealers_envelop);
                            break;

                        default:
                            // 跳转到红包页面
                            intent = new Intent(mInflater.getContext(), DanluEnvelopeActivity.class);
                            break;
                    }
                } else { // 其他消息
                    intent = new Intent(mInflater.getContext(), OtherDetailMsgActivity.class);
                    bundle.putString("time", data.getBizCreatedTimestamp().substring(0, 10));
                    bundle.putString("content", data.getContent());
                    intent.putExtras(bundle);
                }
                if (null != intent) {
                    mInflater.getContext().startActivity(intent);
                }
            }
        });
        return convertView;
    }

    private static class ViewHolder {
        TextView tvTime;
        TextView tvTitle;
        TextView tvContent;
        TextView tvAction;
        LinearLayout llMore;
        View viewToken;

        ViewHolder(View view) {
            tvTime = (TextView) view.findViewById(R.id.tv_time);
            tvTitle = (TextView) view.findViewById(R.id.tv_title);
            tvContent = (TextView) view.findViewById(R.id.tv_content);
            tvAction = (TextView) view.findViewById(R.id.tv_action);
            llMore = (LinearLayout) view.findViewById(R.id.ll_view_more);
            viewToken = view.findViewById(R.id.view_token);
        }
    }
}